package cn.wen.yinxiu.product.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.product.vo.AttrInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.product.dao.AttrDao;
import cn.wen.yinxiu.product.entity.AttrEntity;
import cn.wen.yinxiu.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrInfoVO> getAttrInfoListBySkuId(Long productId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", productId);
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        queryWrapper.eq("status", StatusEnum.NORMAL_USE.code());
        List<AttrEntity> attrEntities = attrDao.selectList(queryWrapper);
        if (attrEntities != null && attrEntities.size() > 0) {
            return attrEntities.stream().map(item -> {
                AttrInfoVO target = new AttrInfoVO();
                BeanUtils.copyProperties(item, target);
                return target;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

}